package com.ketai.web;

import com.ketai.common.MessageConstant;
import com.ketai.common.Result;
import com.ketai.common.StatusCode;
import com.ketai.pojo.Evaluation;
import com.ketai.pojo.Organization;
import com.ketai.service.EvaluationService;
import com.ketai.service.impl.EvaluationServiceImpl;
import com.ketai.util.Pages;
import com.ketai.util.SendUtil;
import com.ketai.vo.EvaluationVo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: 高伟华
 * @Description:
 * @Date:Create In 2022-08-11 13:47
 **/
@WebServlet("/admin/evaluation")
public class EvaluationServlet extends HttpServlet {
    EvaluationService evaluationService = new EvaluationServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String opr = req.getParameter("opr");
        if ("list".equals(opr)) {
            List<EvaluationVo> evaluationVoList = evaluationService.getEvaluation();
            Pages<EvaluationVo> page = new Pages<>();
            page.setDataItems(evaluationVoList);
            page.setDataTotal(10L);
            SendUtil.SendPageJson(resp, page);

        } else if ("del".equals(opr)) {
            String id = req.getParameter("id");
            int result = evaluationService.deleteEvaluationById(id);
            Result r = Result.message(MessageConstant.EVALUATE_DELETE_SUCCESS);
            if (result == 0) {
                r = Result.error(StatusCode.SQLERROR, MessageConstant.EVALUATE_DELETE_ERROR);
            }

            }else if ("byid".equals(opr)) {
                String id = req.getParameter("id");
                EvaluationVo evaluationvo = evaluationService.getEvaluationById(id);
                SendUtil.SendjsonFormart(resp, evaluationvo);
        }else if("check".equals(opr)){
            String id  = req.getParameter("id");
            String open = req.getParameter("open");
            String desc =  req.getParameter("desc");
            String pid = "1";
            int result  = evaluationService.checkEvaluation(id,open,desc);
            if(result>0){
                SendUtil.Sendjson(resp,MessageConstant.ACTITY_CHECK_SUCCESS);
            }else{
                SendUtil.Sendjson(resp,MessageConstant.ACTITY_CHECK_SUCCESS);
            }

        }
    }
}
